using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Reflection;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Tasks;

namespace VTGGoldMiner
{
    //lop chi chua cac ham duoc su dung nhieu lan. khong lien quan den ti le
    class CacHam
    {
        private GraphicsDevice dohoa;
        private Texture2D diemve;
        public CacHam(GraphicsDevice khungve)
        {
            dohoa = khungve;
            diemve = new Texture2D(dohoa, 1, 1, false, SurfaceFormat.Color);
        }
        public void LuuDuLieu(string fileluu, string dulieu)
        {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            try
            {
                using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream(fileluu, FileMode.Create, FileAccess.Write, myIsolatedStorage)))
                {
                    string someTextData = dulieu;
                    writeFile.WriteLine(someTextData);
                    writeFile.Close();
                }
            }
            catch { }
        }
        public string DocDuLieu(string fileluu)
        {
            string ketqua = "0";
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            try
            {
                IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fileluu, FileMode.Open, FileAccess.Read);
                using (StreamReader reader = new StreamReader(fileStream))
                {
                    ketqua = reader.ReadLine();
                }
            }
            catch { }
            return ketqua;
        }
        public Boolean QuangBaSanPham()
        {
            Boolean ketqua = false;
            try
            {
                MarketplaceReviewTask trangdanhgia = new MarketplaceReviewTask();
                trangdanhgia.Show();
                ketqua = true;
            }
            catch
            {
                ketqua = false;
            }
            return ketqua;
        }
        public Rectangle BoHinhAnh(int tdx, int tdy, int rong, int dai, float tilerong, float tiledai)
        {
            int tdxmoi = Convert.ToInt32(tdx * tilerong);
            int tdymoi = Convert.ToInt32(tdy * tiledai);
            int rongmoi = Convert.ToInt32(rong * tilerong);
            int daimoi = Convert.ToInt32(dai * tiledai);
            return new Rectangle(tdxmoi, tdymoi, rongmoi, daimoi);
        }
        public Boolean KiemTraChua(Vector2 diem, Rectangle vungchua)
        {
            Boolean ketqua = false;
            if (diem.X >= vungchua.X && diem.X <= vungchua.X + vungchua.Width && diem.Y >= vungchua.Y && diem.Y <= vungchua.Y + vungchua.Height)
                ketqua = true;
            return ketqua;
        }
        public Boolean KiemTraGiaoHCN(Rectangle hcn1, Rectangle hcn2)
        {
            Boolean ketqua = false;
            Rectangle hcngiaonhau = new Rectangle();
            hcngiaonhau = Rectangle.Intersect(hcn1, hcn2);
            if (!hcngiaonhau.IsEmpty)
            {
                ketqua = true;
            }
            return ketqua;
        }
        public void XoayHinhAnh(SpriteBatch nenve, Texture2D hinhanh, int gocxoay, Rectangle boanh, Vector2 tamxoay, float tiledanrong, float tiledandai)
        {
            boanh.X = boanh.X + Convert.ToInt32(tamxoay.X * tiledanrong);
            boanh.Y = boanh.Y + Convert.ToInt32(tamxoay.Y * tiledandai);
            //tam xoay phai tinh theo kich thuoc cua hinh anh
            nenve.Draw(hinhanh, boanh, null, Color.White, gocxoay * (float)Math.PI / 180, tamxoay, SpriteEffects.None, 0f);
        }
        public void VeDuongThang(SpriteBatch nenve, float doday, Color mau, Vector2 diema, Vector2 diemb)
        {
            diemve.SetData(new[] { Color.White });
            float angle = (float)Math.Atan2(diemb.Y - diema.Y, diemb.X - diema.X);
            float length = Vector2.Distance(diema, diemb);
            nenve.Draw(diemve, diema, null, mau, angle, Vector2.Zero, new Vector2(length, doday), SpriteEffects.None, 0);
        }
    }
}